﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ThuVien;
using System.Net.Mail;
namespace TranTekBE.Maintenance
{
    public partial class Add : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRequest request = base.Request;
            string address = request.UserHostAddress;

            username.Text = UserInfo.UserName;
            fullname.Text = UserInfo.FirstName + " " + UserInfo.LastName;
            if (!IsPostBack)
            {
                /*
                 * Lấy danh sách phòng ban ( gồm id, tên phòng, email )
                 */
                DataSet listdeptid = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_PhongBan");
                if (listdeptid != null)
                {
                    FunctionsUtils.BindDataForControlList(deptid, listdeptid, "ID", "TenPhong");
                }
                /*
                * Lấy danh sách cấp độ ( gồm id, cấp độ )
                */

                DataSet listpriority = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_CapDo");
                if (listpriority != null)
                {
                    FunctionsUtils.BindDataForControlList(priority, listpriority, "ID", "CapDo");
                }
                /*
                 * Lấy danh sách cửa hàng ( gồm id và tên )
                 */
                DataSet liststore = FunctionsUtils.Database.ExecuteQuery("SELECT C007000 as ID, C007001 as Name FROM T0070");
                if (liststore != null)
                {
                    FunctionsUtils.BindDataForControlList(storeid, liststore, "ID", "Name");
                }
            }
        }

        protected void submitrequest_Click(object sender, EventArgs e)
        {
            int IDPhongBan = int.Parse(deptid.SelectedValue);
            string IDNguoiGui = UserInfo.UserName;
            int IDCapDo = int.Parse(priority.SelectedValue);
            string IDCuaHang = storeid.SelectedValue;
            string title = subject.Text;
            string messagerequest = TextToHtml(message.Text);
            string now = DateTime.Now.ToString();
            if(IDPhongBan >= 0 && IDCapDo >= 0 && messagerequest.Length > 0 && title.Length > 0)
            {
                if (messagerequest.Length <= 3000 && title.Length <= 1000)
                {
                    messagerequest = messagerequest.Replace("'", "");
                    title = title.Replace("'", "");
                    string sql = "INSERT INTO tbl_YeuCau "
                        + "VALUES(N'" + title + "'," + IDPhongBan + ",N'" + messagerequest + "',N'" + now + "',N'" + IDNguoiGui + "'," + IDCapDo + ",1,-1,N'" + now + "',N'"+IDCuaHang+"')";
                    int done = FunctionsUtils.Database2.ExecuteNonQuery(sql);
                    statusrequest.Text = "<font color='green'>Thêm yêu cầu mới thành công</font>";
                    DataSet maxid = FunctionsUtils.Database2.ExecuteQuery("SELECT TOP 1 * FROM tbl_YeuCau ORDER BY ID DESC");
                    string idnew = "";
                    if (maxid != null)
                    {
                        foreach (DataRow Rows in maxid.Tables[0].Rows)
                        {
                            idnew = Rows[0].ToString();
                            
                        }
                    }
                    /*
                     * Send mail
                     */
                    subject.Text = "[" + storeid.SelectedItem.ToString() + "][" + priority.SelectedItem.ToString() + "][" + deptid.SelectedItem.ToString() + "]" + subject.Text;
                    message.Text = "<h2>Thông báo từ nhà thuốc: " + storeid.SelectedItem.ToString() + " (" + priority.SelectedItem.ToString() + ")</h2><br><b>" + message.Text + "</b>";
                    message.Text += "<br>Chi tiết xem tại : <a href=\"http://employee.pharmacity.vn/Maintenance/Allrequest.aspx\"> ID = "+idnew+"</a>";

                    senmail(IDCapDo);
                    nullsubject.Text = "";
                    nullmsg.Text = "";
                    subject.Text = "";
                    message.Text = "";


                    
                    //Response.Redirect("Maintenance.aspx");
                }
                else
                {
                    statusrequest.Text = "<font color='red'>Lổi: Nội dung quá dài.</font>";
                    if (messagerequest.Length > 3000)
                        nullmsg.Text = "<font color='red'>Lổi: Dữ liệu quá dài</font>";
                    else
                        nullmsg.Text = "";
                    if (title.Length > 1000)
                        nullsubject.Text = "<font color='red'>Lổi: Dữ liệu quá dài</font>";
                    else
                        nullsubject.Text = "";
                }
            }
            else
            {
                if (messagerequest.Length == 0)
                    nullmsg.Text = "<font color='red'>Lổi: Dữ liệu bắt buộc</font>";
                else
                    nullmsg.Text = "";
                if (title.Length == 0)
                    nullsubject.Text = "<font color='red'>Lổi: Dữ liệu bắt buộc</font>";
                else
                    nullmsg.Text = "";
                statusrequest.Text = "<font color='red'>Lổi: Nội dung chưa đầy đủ.</font>";
            }
        }

        protected void rehome_Click(object sender, EventArgs e)
        {
            Response.Redirect("Maintenance.aspx",true);
        }

        protected void addnew_Click(object sender, EventArgs e)
        {
            Response.Redirect("Allrequest.aspx", true);
        }
        public string TextToHtml(string text)
        {
            text = HttpUtility.HtmlEncode(text);
            text = text.Replace("\r\n", "\r");
            text = text.Replace("\n", "\r");
            text = text.Replace("\r", "<br>\r\n");
            text = text.Replace("  ", " &nbsp;");
            return text;
        }
        public void senmail(int capdo)
        {
            MailMessage messages = new MailMessage();

            messages.From = new MailAddress("sydinh.pharmacity@gmail.com");
            if(capdo == 3)
                messages.To.Add(new MailAddress("thienle@pharmacity.vn"));
            messages.To.Add(new MailAddress("sydinh.pharmacity@gmail.com"));
            messages.CC.Add(new MailAddress("dinhnguyen.pharmacity@gmail.com"));
            messages.CC.Add(new MailAddress("trampham.pharmacity@gmail.com"));
            messages.CC.Add(new MailAddress("doankhanh.pharmacity@gmail.com"));
            messages.Subject = subject.Text;
            messages.Body = message.Text;
            messages.IsBodyHtml = true;
            messages.Priority = MailPriority.Normal;

            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential("sydinh.pharmacity@gmail.com", "241325783");

            client.Port = 587;

            client.Host = "smtp.gmail.com";

            client.EnableSsl = true;

            client.Send(messages); 
        }
    }
}